/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.panels;

import Components.FileType;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import laminaatapplicatie.Global;

/**
 *
 * @author Memet
 */
public class ADPLViewPanel extends javax.swing.JPanel implements ClipboardOwner {
    String text;
    Clipboard clipboard;
    FileType type;
    /**
     * Creates new form ADPLViewPanel
     */
    public ADPLViewPanel() {
        initComponents();
        text = "";
        txtOutput.setText(text);
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        type =FileType.ALL;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtOutput = new javax.swing.JTextPane();
        btnCopy = new javax.swing.JButton();
        btnExportToFile = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tpMessages = new javax.swing.JTextPane();

        jScrollPane1.setViewportView(txtOutput);

        btnCopy.setText("Copy to clipboard");
        btnCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCopyActionPerformed(evt);
            }
        });

        btnExportToFile.setText("Export to file");
        btnExportToFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportToFileActionPerformed(evt);
            }
        });

        tpMessages.setBorder(javax.swing.BorderFactory.createTitledBorder("Messages"));
        tpMessages.setContentType("text/html");
        jScrollPane2.setViewportView(tpMessages);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(btnCopy)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExportToFile)
                .addGap(224, 224, 224))
            .addComponent(jScrollPane1)
            .addComponent(jScrollPane2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCopy)
                    .addComponent(btnExportToFile))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCopyActionPerformed
        clipboard.setContents(new StringSelection(txtOutput.getText()), this);
    }//GEN-LAST:event_btnCopyActionPerformed

    private void btnExportToFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportToFileActionPerformed
        //Global.mainForm.saveProgram();
        String filter = "*.*";
        String desc = "General file";
        if (type.equals(FileType.LCP)) {
            filter = "*.lcp";
            desc = "Laminate Calculation Program file";
        }
        if (type.equals(FileType.LCPMAT)) {
            filter = "*.lcpmat";
            desc = "Laminate Calculation Program Material file";
        }
        if (type.equals(FileType.MP)) {
            filter = "*.mp";
            desc = "Ansys Material file";
        }
        String filename = "";
        JFileChooser fc = new JFileChooser();
        
        fc.setDialogTitle("Export to " + desc);
        fc.setApproveButtonText("Save");
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            BufferedWriter out = null;
            try {
                File file = fc.getSelectedFile();
                filename = file.getPath();
                out = new BufferedWriter(new FileWriter(file));
                out.write(txtOutput.getText());
                out.close();
            } catch (IOException ex) {
                //Logger.getLogger(AnsysXMLBuilderForm.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    out.close();
                } catch (IOException ex) {
                    //Logger.getLogger(AnsysXMLBuilderForm.class.getName()).log(Level.SEVERE, null, ex);
                }
            }



        }
    }//GEN-LAST:event_btnExportToFileActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCopy;
    private javax.swing.JButton btnExportToFile;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane tpMessages;
    private javax.swing.JTextPane txtOutput;
    // End of variables declaration//GEN-END:variables

    @Override
    public void lostOwnership(Clipboard clpbrd, Transferable t) {
         
    }
    
    public String getText() {
        return txtOutput.getText();
    }

    public void setText(String text) {
        this.text = text;
        txtOutput.setText(text);
    }

    public FileType getType() {
        return type;
    }

    public void setType(FileType type) {
        this.type = type;
    }

    public void setMessage(String message)
    {
        tpMessages.setText(message);
        tpMessages.repaint();
    }

    public String getMessage()
    {
        return tpMessages.getText();
    }
}
